package logics

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/rpcPay/common/lib"
	"git.900sui.cn/kc/rpcPay/common/lib/goftp"
	pay2 "git.900sui.cn/kc/rpcinterface/interface/pay"
	"strconv"
)

type FileLogic struct {
}

func (this *FileLogic) InitGofpConfig() *goftp.ConnectCfg {
	address := fmt.Sprintf("%s:%s", kcgin.AppConfig.String("xindalu.hZ.ftp.address"), kcgin.AppConfig.String("xindalu.hZ.ftp.port"))
	username := kcgin.AppConfig.String("xindalu.hZ.ftp.username")
	pwd := kcgin.AppConfig.String("xindalu.hZ.ftp.pwd")
	cfg := goftp.NewCfg(
		goftp.WithAddress(address),
		goftp.WithUserName(username),
		goftp.WithPwd(pwd))
	return cfg
}
func (this *FileLogic) InitGoftp(cfg *goftp.ConnectCfg, path string, dirpath string) (*goftp.Ftp, error) {
	disDir := kcgin.AppConfig.String("xindalu.hZ.ftp.path") //目的地址信息
	if dirpath != "" {
		disDir = dirpath
	}
	disDir = fmt.Sprintf("%s%s", disDir, path)
	ftp, err := goftp.Init(cfg, disDir)
	if err != nil {
		return nil, err
	}
	return ftp, nil
}
func (this *FileLogic) FileDownload(ctx context.Context, args *pay2.ArgsReplyDownload, reply *bool) error {
	err := this.HandlerCid(args.Cid) //地区校验
	if err != nil {
		return err
	}
	filename := args.Format()                              //参数格式化文件名称
	cfg := this.InitGofpConfig()                           //初始化注入ConnectConfig 配置信息
	goftp, err := this.InitGoftp(cfg, filename, args.Path) //初始化goft客户端 path文件名称
	if err != nil {
		return err
	}
	if goftp != nil {
		defer goftp.Close()
	}
	fmt.Println(filename)
	isTrue, err := goftp.DownLoad(filename)
	if err != nil {
		return err
	}
	*reply = isTrue
	return nil

}
func (this *FileLogic) HandlerCid(cid string) error {
	cidInt, _ := strconv.Atoi(cid)
	if cidInt != lib.CHANNELAREA_xindalu {
		return errors.New("只有新大陆卡通")
	}
	return nil
}
func (this *FileLogic) SplitaccountDownload(ctx context.Context, args *pay2.ArgsSplitaccount, reply *bool) error {
	err := this.HandlerCid(args.Cid) //地区校验
	if err != nil {
		return err
	}
	filename := args.FormatFenzhang() //参数格式化
	cfg := this.InitGofpConfig()      //初始化注入ConnectConfig 配置信息
	goftp, err := this.InitGoftp(cfg, filename, args.Path)
	if err != nil {
		return err
	}
	if goftp != nil {
		defer goftp.Close()
	}
	isTrue, err := goftp.DownLoad(filename)
	if err != nil {
		return err
	}
	*reply = isTrue
	return nil
}

//分账信息
func (this *FileLogic) FenzhangInfo(ctx context.Context, args *pay2.ArgsSplitaccount, reply *pay2.ReplyFenzhangInfoInfo) error {
	err := this.HandlerCid(args.Cid) //地区校验
	if err != nil {
		return err
	}
	filename := args.FormatFenzhang() //参数格式化
	cfg := this.InitGofpConfig()      //初始化注入ConnectConfig 配置信息
	goftp, err := this.InitGoftp(cfg, filename, args.Path)
	if err != nil {
		return err
	}
	bytesBuffer, err := goftp.Content(filename)
	if err != nil {
		return err
	}
	isFirst := true
	countIndex := 0
	Total := 0
	for {
		c, _ := bytesBuffer.ReadBytes('\n')
		//下面是第一次
		if isFirst {
			count := &pay2.ReplySplitaccountCount{}
			json.Unmarshal(c, count)
			reply.SUM_COUNT = count.SUM_COUNT
			Total = count.SUM_COUNT
		}
		if countIndex == Total+1 {
			break
		}
		if !isFirst {
			//如果不是第一次 则进行其他信息
			fenzhaninfo := new(pay2.ReplySplitaccountDownload)
			json.Unmarshal(c, fenzhaninfo)
			reply.Res = append(reply.Res, fenzhaninfo)
		}
		isFirst = false
		countIndex++
	}
	return nil
}
func (this *FileLogic) DuizhangInfo(ctx context.Context, args *pay2.ArgsReplyDownload, reply *pay2.ReplyDuizhangInfo) error {
	err := this.HandlerCid(args.Cid) //地区校验
	if err != nil {
		return err
	}
	filename := args.Format()    //参数格式化
	cfg := this.InitGofpConfig() //初始化注入ConnectConfig 配置信息
	goftp, err := this.InitGoftp(cfg, filename, args.Path)
	if err != nil {
		return err
	}
	bytesBuffer, err := goftp.Content(filename)
	if err != nil {
		return err
	}
	isFirst := true
	countIndex := 0
	Total := 0
	for {
		c, _ := bytesBuffer.ReadBytes('\n')
		//下面是第一次
		if isFirst {
			count := &pay2.ReplyDuizhangSumCount{}
			json.Unmarshal(c, count)
			reply.SUM_COUNT = count.SUM_COUNT
			reply.RE_SUM_COUNT = count.RE_SUM_COUNT
			reply.RE_SUM_OLD_TXAMT = count.RE_SUM_OLD_TXAMT
			reply.SUM_CUST_AMT = count.SUM_CUST_AMT
			reply.SUM_FEE = count.SUM_FEE
			reply.SUM_TXAMT = count.SUM_TXAMT
			reply.SUM_OLD_TXAMT = count.SUM_OLD_TXAMT
			Total, _ = strconv.Atoi(count.SUM_COUNT)
		}
		if countIndex == Total+1 {
			break
		}
		if !isFirst {
			fenzhaninfo := new(pay2.ReplyDuizhan)
			json.Unmarshal(c, fenzhaninfo)
			reply.Res = append(reply.Res, fenzhaninfo)
		}
		isFirst = false
		countIndex++

	}
	return nil
}
